package com.zkzx.company.base.service.constants;

import com.zkzx.common.company.command.CompanyCommand;
import com.zkzx.common.company.model.CompanyFinanceVO;
import com.zkzx.common.company.model.CompanyOperationVO;
import com.zkzx.company.base.mapper.entity.Company;
import com.zkzx.company.base.mapper.entity.CompanyFinance;
import com.zkzx.company.base.mapper.entity.CompanyOperation;
import com.zkzx.company.base.service.CompanyBuildSatisfy;
import com.zkzx.company.base.service.domian.GatherCompany;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月29日 15时56分
 */
@Getter
@AllArgsConstructor
public enum CompanyStatusEnum implements CompanyBuildSatisfy {

    /**
     * 不存在
     */
    NO_EXIST(200,"待提交"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command,Integer userId,GatherCompany gatherCompany) {
            return GatherCompany.builder()
                    .company(this.buildCompany(command,userId,gatherCompany))
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },

    /**
     * 存在
     */
    EXIST(200,"存在"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command,Integer userId,GatherCompany gatherCompany) {

            Company company = gatherCompany.getCompany();
            company.setThreeid(command.getThreeId());
            company.setStatus(Optional.ofNullable(command.getStatusVO().getValue()).orElse(company.getStatus()));
            company.setLastmodifiedtime(new Date());
            company.setLastmodifieduser(userId);

            return GatherCompany.builder()
                    .company(company)
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },


    /**
     * 审核通过
     */
    REVIEW(204,"审核通过"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany) {
            return GatherCompany.builder()
                    .company(this.buildCompany(command,userId,gatherCompany))
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },

    /**
     * 审核未通过
     */
    NO_REVIEW(401,"审核未通过"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany) {

            return GatherCompany.builder()
                    .company(this.buildCompany(command,userId,gatherCompany))
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },

    /**
     * 审核中
     */
    WAIT(205,"审核中"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany) {

            Company company = gatherCompany.getCompany();
            company.setStatus(Optional.ofNullable(command.getStatusVO().getValue()).orElse(company.getStatus()));
            company.setLastmodifiedtime(new Date());
            company.setLastmodifieduser(userId);
            company.setThreeid(Optional.ofNullable(command.getThreeId()).orElse(company.getThreeid()));

            return GatherCompany.builder()
                    .company(company)
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },

    /**
     * 启用
     */
    ENABLE(6,"启用"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany) {
            return GatherCompany.builder()
                    .company(this.buildCompany(command,userId,gatherCompany))
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },

    /**
     * 禁用
     */
    DISABLE(7,"禁用"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany) {
            return GatherCompany.builder()
                    .company(this.buildCompany(command,userId,gatherCompany))
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    },

    /**
     * 注销
     */
    OFF(-1,"注销"){
        @Override
        public GatherCompany buildGatherCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany) {
            return GatherCompany.builder()
                    .company(this.buildCompany(command,userId,gatherCompany))
                    .companyFinance(this.buildCompanyFinance(command,gatherCompany))
                    .companyOperation(this.buildCompanyOperationVO(command,gatherCompany))
                    .build();
        }
    };

    private int value;

    private String description;






    Company buildCompany(CompanyCommand command, Integer userId,GatherCompany gatherCompany){
        Company company = gatherCompany.getCompany();
        if(Objects.isNull(company)){
            company = new Company();
        }
        company.setId(command.getId());
        company.setSource(Optional.ofNullable(command.getSource()).orElse(company.getSource()));
        company.setCid(Optional.ofNullable(company.getCid()).orElse(company.getCid()));
        command.setCode(Optional.ofNullable(command.getCode()).orElse(company.getCode()));
        company.setName(Optional.ofNullable(command.getName()).orElse(company.getName()));
        company.setType(Optional.ofNullable(command.getType()).orElse(company.getType()));
        company.setPerson(Optional.ofNullable(command.getPerson()).orElse(company.getPerson()));
        company.setRegionid(Optional.ofNullable(command.getRegionId()).orElse(company.getRegionid()));
        company.setRegioninfo(Optional.ofNullable(command.getRegionInfo()).orElse(company.getRegioninfo()));
        company.setRegaddress(Optional.ofNullable(command.getRegAddress()).orElse(company.getRegaddress()));
        company.setRegtime(Optional.ofNullable(command.getRegTime()).orElse(company.getRegtime()));
        company.setTaxauthorities(Optional.ofNullable(command.getTaxAuthorities()).orElse(company.getTaxauthorities()));
        company.setRegamount(Optional.ofNullable(command.getRegAmount()).orElse(company.getRegamount()));
        company.setFoundtime(Optional.ofNullable(command.getFoundTime()).orElse(company.getFoundtime()));
        company.setScope(Optional.ofNullable(command.getScope()).orElse(company.getScope()));
        company.setBusinesspic(Optional.ofNullable(command.getBusinessPicURL()).orElse(company.getBusinesspic()));
        company.setLicensepic(Optional.ofNullable(command.getLicensePicURL()).orElse(company.getLicensepic()));
        company.setAuthorizepic(Optional.ofNullable(command.getAuthorizePicURL()).orElse(company.getAuthorizepic()));
        company.setStatus(Optional.ofNullable(command.getStatusVO().getValue()).orElse(company.getStatus()));
        company.setContractcode(Optional.ofNullable(command.getContractCode()).orElse(company.getContractcode()));
        company.setMobile(Optional.ofNullable(command.getMemberMobile()).orElse(company.getMobile()));
        company.setContractstarttime(Optional.ofNullable(command.getContractStartTime()).orElse(company.getContractstarttime()));
        company.setContractendtime(Optional.ofNullable(command.getContractEndTime()).orElse(company.getContractendtime()));
        company.setContractfile(Optional.ofNullable(command.getContractFileURL()).orElse(company.getContractfile()));
        company.setMembercode(Optional.ofNullable(command.getAccountNumber()).orElse(company.getMembercode()));
        company.setRemark(Optional.ofNullable(command.getRemark()).orElse(company.getRemark()));
        company.setEmail(Optional.ofNullable(command.getEmail()).orElse(company.getEmail()));
        company.setReason(company.getReason());
        company.setLastmodifiedtime(new Date());
        company.setLastmodifieduser(userId);
        company.setProvince(Optional.ofNullable(command.getProvince()).orElse(company.getProvince()));
        company.setCity(Optional.ofNullable(command.getCity()).orElse(company.getCity()));
        company.setCounty(Optional.ofNullable(command.getCounty()).orElse(company.getCounty()));
        company.setThreeid(Optional.ofNullable(command.getThreeId()).orElse(company.getThreeid()));
        return company;
    }

    CompanyOperation buildCompanyOperationVO(CompanyCommand command,GatherCompany gatherCompany){
        CompanyOperationVO operation = command.getCompanyOperation();
        CompanyOperation companyOperation = gatherCompany.getCompanyOperation();
        if(Objects.isNull(operation)){
            return companyOperation;
        }
        if(Objects.isNull(companyOperation)){
            companyOperation = new CompanyOperation();
        }
        companyOperation.setCompanyid(command.getId());
        companyOperation.setName(Optional.ofNullable(operation.getName()).orElse(companyOperation.getName()));
        companyOperation.setMobile(Optional.ofNullable(operation.getMobile()).orElse(companyOperation.getMobile()));
        companyOperation.setRegionid(Optional.ofNullable(operation.getRegionId()).orElse(companyOperation.getRegionid()));
        companyOperation.setRegioninfo(Optional.ofNullable(operation.getRegionInfo()).orElse(companyOperation.getRegioninfo()));
        companyOperation.setAddress(Optional.ofNullable(operation.getAddress()).orElse(companyOperation.getAddress()));
        companyOperation.setFarescale(Optional.ofNullable(operation.getFareScale()).orElse(companyOperation.getFarescale()));
        companyOperation.setProvince(Optional.ofNullable(operation.getProvince()).orElse(companyOperation.getProvince()));
        companyOperation.setCity(Optional.ofNullable(operation.getCity()).orElse(companyOperation.getCity()));
        companyOperation.setCounty(Optional.ofNullable(operation.getCounty()).orElse(companyOperation.getCounty()));
        return companyOperation;
    }

    CompanyFinance buildCompanyFinance(CompanyCommand command,GatherCompany gatherCompany){
        CompanyFinanceVO finance = command.getCompanyFinance();
        CompanyFinance companyFinance = gatherCompany.getCompanyFinance();
        if(Objects.isNull(finance)){
            return companyFinance;
        }
        if(Objects.isNull(companyFinance)){
            companyFinance = new CompanyFinance();
        }
        companyFinance.setCompanyid(command.getId());
        companyFinance.setBank(Optional.ofNullable(finance.getBank()).orElse(companyFinance.getBank()));
        companyFinance.setBankamount(Optional.ofNullable(finance.getBankAmount()).orElse(companyFinance.getBankamount()));
        return companyFinance;
    }

}
